#include "byteplot.h"

byteplot::byteplot(QWidget *parent)
  : QMainWindow(parent)
  //  : QWidget(parent)
{
  plotWidget = new Plot;
  plotWidget->setMinimumSize(200,200);
  statusBar = new QStatusBar;
  statusBar->showMessage("Connect to a data source");

  this->setCentralWidget(plotWidget);

  createDocks();
  createMenus();
  createConnections();

  this->setStatusBar(statusBar);
  this->setWindowTitle("flepp // FLExible Parse & Plot");

}

byteplot::~byteplot()
{

}

void byteplot::connectionChanged(bool conn)
{
  if(conn)
  {
    // This is *very* preliminary
    currentState = CONNECTED;
    statusBar->showMessage("Connected to data source");
  }
  else
  {
    currentState = NONE;
    statusBar->showMessage("Disconnected from data source");
  }
}

void byteplot::recBytes(QByteArray datain)
{
  // Distribute bytes received from the data connection:
  // 1. Parser
  parserWidget->datafromServer(datain);
  // 2. Logger

  //        temp.append("<--");
  //        temp.append(datain);
  //        terminal01->append(temp);
  //        parserWidget->parser->dataparse(datain);

}

void byteplot::createDocks()
{
  // Set up docks:
  // 1. Connection
  connectionDock = new QDockWidget(tr("Connection"));
  connectionWidget = new QConnectionWidget(connectionDock);
  //sourceWidget = new QDataWidget(sourceDock);
  connectionDock->setWidget(connectionWidget);
  connectionDock->setFeatures(QDockWidget::DockWidgetClosable|
                          QDockWidget::DockWidgetMovable|
                          QDockWidget::DockWidgetFloatable);
  connectionDock->setAllowedAreas(Qt::AllDockWidgetAreas);
  addDockWidget(Qt::TopDockWidgetArea,connectionDock);
  connectionDock->setFixedSize(300,205);

  // 2. Terminal
  terminalDock = new QDockWidget(tr("Terminal"));
  terminalWidget = new QTerminalWidget(terminalDock);
  terminalDock->setWidget(terminalWidget);
  terminalDock->setFeatures(QDockWidget::DockWidgetClosable|
                            QDockWidget::DockWidgetMovable|
                            QDockWidget::DockWidgetFloatable);
  terminalDock->setAllowedAreas(Qt::AllDockWidgetAreas);
  addDockWidget(Qt::TopDockWidgetArea,terminalDock);
  terminalDock->setMinimumSize(400,205);
  terminalDock->resize(400,205);
  //terminalDock->setFixedSize(276,205);

  // 3. Parser
  parserDock = new QDockWidget(tr("Parser"));
  parserWidget = new QParserWidget(parserDock);
  parserDock->setWidget(parserWidget);
  parserDock->setFeatures(QDockWidget::DockWidgetClosable|
                          QDockWidget::DockWidgetMovable|
                          QDockWidget::DockWidgetFloatable);
  parserDock->setAllowedAreas(Qt::AllDockWidgetAreas);
addDockWidget(Qt::TopDockWidgetArea,parserDock);

  tabifyDockWidget(connectionDock,terminalDock);

  QList<QTabBar *> tabList = findChildren<QTabBar *>();
  if(!tabList.isEmpty()){
    QTabBar *tabBar = tabList.at(0);
    tabBar->setCurrentIndex(0);
    QString currentTab = tabBar->tabText(tabBar->currentIndex());
    qDebug() << currentTab;
  }

  // 5. Plotting
  plotsetsDock = new QDockWidget(tr("Plot"));
  plotsetsWidget = new QPlotSettings(plotsetsDock);
  plotsetsDock->setWidget(plotsetsWidget);
  plotsetsDock->setFeatures(QDockWidget::DockWidgetClosable|
                            QDockWidget::DockWidgetMovable|
                            QDockWidget::DockWidgetFloatable);
  plotsetsDock->setAllowedAreas(Qt::AllDockWidgetAreas);
  plotsetsDock->setMinimumSize(350,300);
  plotsetsDock->setMaximumWidth(350);
  addDockWidget(Qt::LeftDockWidgetArea,plotsetsDock);


  this->setTabPosition(Qt::AllDockWidgetAreas,QTabWidget::North);

  //  parserLayout->addWidget(parserWidget);
  //  tabWidget->addTab(parserPage,"Parser");

  // 3. Calculations
  //    QWidget *calcPage = new QWidget(this);
  //    QHBoxLayout *calcLayout = new QHBoxLayout(calcPage);
  //    calcWidget = new QCalcWidget(calcPage);
  //    calcLayout->addWidget(calcWidget);
  //    tabWidget->addTab(calcWidget,"Calculations");

  // 4. Logging
  //    QWidget *logPage = new QWidget(this);
  //    QHBoxLayout *logLayout = new QHBoxLayout(logPage);
  //    logWidget = new QLogWidget(logPage);
  //    logLayout->addWidget(logWidget);
  //    tabWidget->addTab(logWidget,"Logging");
}

void byteplot::createMenus()
{
  QMenu *fileMenu = menuBar()->addMenu(tr("File"));
  exitAct = new QAction(tr("E&xit"), this);
       exitAct->setStatusTip(tr("Exit the application"));
       connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
  fileMenu->addAction(exitAct);
  QMenu *viewMenu = menuBar()->addMenu(tr("View"));
  viewMenu->addAction(connectionDock->toggleViewAction());
  viewMenu->addAction(terminalDock->toggleViewAction());
  viewMenu->addAction(parserDock->toggleViewAction());
viewMenu->addAction(plotsetsDock->toggleViewAction());
}

void byteplot::createConnections()
{
  // Data handling
  // CONNECTION <-> TERMINAL
  connect(connectionWidget,SIGNAL(dataIn(QByteArray)),terminalWidget,SLOT(dataIn(QByteArray)));
  connect(terminalWidget,SIGNAL(dataOut(QByteArray)),connectionWidget,SLOT(dataOut(QByteArray)));

  // CONNECTION -> PARSER
  connect(connectionWidget,SIGNAL(dataIn(QByteArray)),parserWidget,SLOT(datafromServer(QByteArray)));

  // PARSER -> PLOT SETTINGS
  connect(parserWidget,SIGNAL(newVariable(variable)),plotsetsWidget,SLOT(newVariable(variable)));
  connect(parserWidget,SIGNAL(editVariable(int,variable)),plotsetsWidget,SLOT(editVariable(int,variable)));
  connect(parserWidget,SIGNAL(removeVariable(int)),plotsetsWidget,SLOT(removeVariable(int)));
  //connect(parserWidget,SIGNAL(listChanged(QList<variable>)),plotsetsWidget,SLOT(buildTable(QList<variable>)));
  connect(parserWidget,SIGNAL(newSample(QList<variable>)),plotWidget,SLOT(newSample(QList<variable>)));

  //PARSER -> PLOT
  connect(parserWidget,SIGNAL(newVariable(variable)),plotWidget,SLOT(newVariable()));
  connect(parserWidget,SIGNAL(removeVariable(int)),plotWidget,SLOT(removeVariable(int)));

  // PLOT SETTINGS -> PLOT
  connect(plotsetsWidget,SIGNAL(plotSelectionChanged(QList<bool>)),plotWidget,SLOT(plotSelection(QList<bool>)));


//  connect(connectionWidget,SIGNAL(dataIn(QByteArray)),this,SLOT(recBytes(QByteArray)));

  // Settings
  connect(plotsetsWidget,SIGNAL(buffSize(int)),plotWidget,SLOT(newbuffSize(int)));
  connect(plotsetsWidget,SIGNAL(canvasColor(QColor)),plotWidget,SLOT(newcanvasColor(QColor)));
  connect(plotsetsWidget,SIGNAL(gridToggle(bool)),plotWidget,SLOT(toggleGrid(bool)));
  connect(plotsetsWidget,SIGNAL(gridColor(QColor)),plotWidget,SLOT(newgridColor(QColor)));
  connect(plotsetsWidget,SIGNAL(curveColor(QColor)),plotWidget,SLOT(newcurveColor(QColor)));
  connect(plotsetsWidget,SIGNAL(curveWidth(int)),plotWidget,SLOT(newcurveWidth(int)));
  connect(plotsetsWidget,SIGNAL(takeSnapshot()),plotWidget,SLOT(takeSnapshot()));
}
